﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/*
Creator:		
Introduction:	
other:			
*/

public class LookAtCamera : MonoBehaviour
{

    public Camera lookAtCamera;
    public bool lookOnlyOnAwake;

    [Header("选择需要固定的轴")]
    [Tooltip("可以自由选择固定不变的轴，常用的选泽是None或者Y")]
    public SelectXYZ selectXYZ = SelectXYZ.None;

    public void Start()
    {
        if (lookAtCamera == null)
        {
            lookAtCamera = Camera.main;
        }
        if (lookOnlyOnAwake)
        {
            LookCam();
        }
    }

    public void Update()
    {
        if (lookAtCamera == null)
        {
            lookAtCamera = Camera.main;
        }
        else
        {
            if (!lookOnlyOnAwake)
            {
                LookCam();
            }
        }
    }

    public void LookCam()
    {
        Vector3 vector3 = lookAtCamera.transform.position - transform.position;
        switch (selectXYZ)
        {
            case SelectXYZ.x:
                vector3.y = vector3.z = 0.0f;
                break;

            case SelectXYZ.y:
                vector3.x = vector3.z = 0.0f;
                break;

            case SelectXYZ.z:
                vector3.x = vector3.y = 0.0f;
                break;

            case SelectXYZ.None:
                vector3.x = vector3.y = vector3.z = 0.0f;
                break;
        }

        transform.LookAt(lookAtCamera.transform.position - vector3);
    }

    public enum SelectXYZ
    {
        x,
        y,
        z,
        None
    }
}
